package com.o2o.cleaning.month.platform.ebusiness_plat.rongegou

import com.alibaba.fastjson.{JSON, JSONObject}
import org.apache.spark.sql.SparkSession
import org.elasticsearch.spark._

/**
  * @author: gaoyadi
  * @Date: 2018/12/18 15:18
  * @Description:
  * @Modify By:
  */
object RYG_Es {
  def main(args: Array[String]): Unit = {
    val spark = SparkSession.builder()
      .appName("SparkTest")
      .master("local[*]")
      .config("es.nodes", "192.168.1.29")
      .config("es.port", "9200")
      .config("es.net.http.auth.user", "elastic")
      .config("es.net.http.auth.pass", "changeme")
      .config("cluster.name", "O2OElastic")
      .getOrCreate()
    val sc = spark.sparkContext
    val sqlcontext = spark.sqlContext

    sc.hadoopConfiguration.set("fs.s3a.access.key", "GAO7EO9FWKPJ8WFCQDME")
    sc.hadoopConfiguration.set("fs.s3a.secret.key", "LZ0xaHBSYKHaJ9ECDbX9f7zin79UZkXfGoNapRPL")
    sc.hadoopConfiguration.set("fs.s3a.endpoint", "https://obs.cn-north-1.myhuaweicloud.com")

    /** *
      * 需要修改的参数信息
      */
    val clusterName = "hdfs://192.168.2.188:9000"
    val year = "2021"
    val month = "12"
    val platform = "rongyigou"
    val address_good = s"s3a://o2o-dataproces-group/zyf/${year}/${month}/${platform}/good_final"
    val index = s"${year}_${platform}/${platform}_${year}_${month}"
    println(index)
    //var address_good=s"s3a://o2o-dataproces-group/li_yinchao/brand/${month}/${platform}/gov/result/*"

    //    val df =sc.textFile(s"${address_good}")
    val df = spark.read.orc(address_good).toJSON.rdd

    var arr = Array(df)
    for (plat <- arr) {
      val value = plat.map(line => {
        val lines = JSON.parseObject(line)

        /** *
          * 需要解析的字段信息
          */

        var original_cost = lines.getOrDefault("original_cost", "-1").toString
        //
        if (original_cost.contains(",")) {
          original_cost = original_cost.replace(",", "")
        }

        val evaluates = lines.get("evaluates").toString
        var ev = "-1"
        var str = new JSONObject
        if (evaluates.equals("-1")) {
          ev = "{\"fuyi\":\"-1\"}"
        } else {
          str = JSON.parseObject(evaluates)
        }
        if (!ev.contains("fuyi")) {
          lines.put("evaluates", str)
        } else {
          val evs = JSON.parseObject(ev)
          lines.put("evaluates", evs)
        }
        //        //        lines.remove("evaluates")
        lines.put("original_cost", original_cost)
        //        lines.put("salesAmount",(priceText*(sellCount.toDouble)).toDouble.formatted("%.2f"))
        lines.remove("shop_pre_md5")
        lines.remove("md5_id")
        lines.remove("pre_md5")
        lines.remove("platformName_spelling")
        lines.remove("md5_shopId")
        lines.remove("type_xichen")
        lines.remove("gehu_type")
        lines.remove("promotion_info")
        lines.remove("is_pingou")
        lines.remove("promotion_info_3")
        lines.remove("promotion_info_2")
        lines.remove("promotion_info_4")
        lines.remove("promotion_info_1")
        //        lines.remove("Base_Info")
        lines.remove("add_to_field")
        lines.remove("company_name")

        //修改 液态奶（100210501）、酸奶（100210502） 中是旺旺 或旺仔 品牌的商品  全部放到 其它牛奶乳品（100210599）中
        val thirdCategoryId = lines.get("thirdCategoryId").toString
        if (thirdCategoryId.equals("100210501") || thirdCategoryId.equals("100210501")) {
          val title = lines.get("title").toString
          if (title.contains("旺旺") || title.contains("旺仔")) {
            lines.put("thirdCategoryId", "100210599")
            lines.put("fourthCategoryId", "10021059999")
          }
        }
        lines
      })
      val time = System.currentTimeMillis()

      /** *
        * 商品店铺入库信息
        */

      if (plat == df) {
        value.saveToEs(index, Map("es.mapping.id" -> "good_id"))
      }

      //      else{
      //        value.saveToEs(s"2019_${platform}_shop/${platform}_shop_2019_${month}",Map("es.mapping.id" -> "shopId"))
      //      }
    }
  }
}
